package com.chen.dao.impl;

import com.chen.dao.NewsDao;
import com.chen.entity.Category;
import com.chen.entity.Comment;
import com.chen.entity.News;
import com.chen.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class NewsDaoImpl implements NewsDao {

    Connection conn=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();

    @Override
    public int save(News news) {
        String sql="INSERT INTO  tb_news(news_id,news_title,news_content,news_time,news_nums,news_type,news_img) VALUES (?,?,?,?,?,?,?)";
        Object[] paras={news.getNews_id(),news.getNews_title(),news.getNews_content(),
                        news.getNews_time(),news.getNews_nums(),news.getNews_type(),news.getNews_img()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(News news) {
        String sql="UPDATE  tb_news SET  news_title=?,news_content=?,news_time=?,news_nums=?,news_type=?,news_img=? WHERE news_id=?  ";
        Object[] paras={news.getNews_title(),news.getNews_content(),
                news.getNews_time(),news.getNews_nums(),news.getNews_type(),news.getNews_img(),news.getNews_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql="DELETE FROM tb_news WHERE news_id  = ?  ";
        Object[] paras={id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public News findById(Object key) {
        String sql="SELECT * FROM tb_news where news_id  = ?";
        Object[] paras={key};
        try {
            News news=qr.query(conn,sql,paras,new BeanHandler<News>(News.class));
            return news;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findAll() {
        String sql="SELECT * FROM tb_news";
        Object[] paras={};
        try {
            List<News> newsList=qr.query(conn,sql,paras,new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findSearch(String keyword) {
        String sql = "SELECT * FROM tb_news where news_title like concat('%',concat(?,'%'))";
        Object[] paras = {keyword};
        try {
            List<News> newstList = qr.query(conn, sql, paras, new BeanListHandler<News>(News.class));
            return newstList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findPage(int page, int pagesize) {
        String sql = "select * from tb_news limit ?,?";
        Object[] paras = {(page-1)*pagesize,pagesize};
        try {
            List<News> commentList = qr.query(conn, sql, paras, new BeanListHandler<News>(News.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql = "select count(*) from tb_news";
        Object[] paras = {};
        try {
            long nums = qr.query(conn, sql, paras, new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
